package com.neuedu.it.newidea.web;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neuedu.it.newidea.po.ReportItem;
import com.neuedu.it.newidea.service.ReportService;
import com.neuedu.it.newidea.web.dto.RespEntity;

import java.util.List;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController


public class ReportController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "/report/publishbyuser/{x}",produces = "application/json;charset=utf-8")
    public String getReportPublishByUser(@PathVariable("x") int rows){
        if(stringRedisTemplate.hasKey("report01"+rows)){
            return (String) stringRedisTemplate.opsForValue().get("report01"+rows);
        }
        List<ReportItem> list = reportService.findReportPublishByUser(rows);
        RespEntity respEntity = new RespEntity("2000","查询成功",list);
        String json = JSON.toJSONString(respEntity);
        stringRedisTemplate.opsForValue().set("report01"+rows,json,1, TimeUnit.HOURS);
        return json;
    }
}
